package com.shujia.day18;

/*
    死锁：线程之间存在相互等待的现象

    案例：中国人和外国人
        前提：中国人吃饭必须要两支筷子，外国人吃饭必须一把刀和一把叉
        现在：
            中国人：一支筷子和一把刀
            外国人：一支筷子和一把叉

 */

class Person extends Thread{
    boolean flag;

    public Person(boolean flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        if(flag){
            synchronized (Locks.LOCK1){
                System.out.println("if lock1");
                // p1
                synchronized (Locks.LOCK2){
                    System.out.println("if lock2");
                }
            }
        }else {
            synchronized (Locks.LOCK2){
                System.out.println("else lock2");
                // p2
                synchronized (Locks.LOCK1){
                    System.out.println("else lock1");
                }
            }
        }
    }
}

public class DieLock {
    public static void main(String[] args) {
        Person p1 = new Person(true);
        Person p2 = new Person(false);

        p1.start();
        p2.start();
    }
}
